package com.example.bigevent.controller;

import com.example.bigevent.pojo.Game;
import com.example.bigevent.pojo.Review;
import com.example.bigevent.pojo.Result;
import com.example.bigevent.service.GameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

@RestController
@RequestMapping("/game")
public class GameController {
    private static final Logger logger = LoggerFactory.getLogger(GameController.class);

    @Autowired
    private GameService gameService;

    @PostMapping
    public Result add(@RequestBody Game game) {
        logger.info("Adding game: {}", game);
        gameService.add(game);
        return Result.success();
    }

    @GetMapping
    public Result<List<Game>> listAll() {
        logger.info("Listing all games");
        List<Game> games = gameService.listAll();
        if (games == null || games.isEmpty()) {
            logger.warn("No games found");
            return Result.success("No games found");
        }
        logger.info("Games found: {}", games);
        return Result.success(games);
    }

    @GetMapping("/{id}")
    public Result<Game> findById(@PathVariable Integer id) {
        logger.info("Finding game by id: {}", id);
        Game game = gameService.findById(id);
        if (game == null) {
            logger.warn("Game not found for id: {}", id);
            return Result.success("Game not found");
        }
        logger.info("Game found: {}", game);
        return Result.success(game);
    }
    
    @GetMapping("/{id}/reviews")
    public Result<List<Review>> findReviewsByGameId(@PathVariable Integer id) {
        logger.info("Finding reviews for game id: {}", id);
        List<Review> reviews = gameService.findReviewsByGameId(id);
        if (reviews == null || reviews.isEmpty()) {
            logger.warn("No reviews found for game id: {}", id);
            return Result.success("No reviews found");
        }
        logger.info("Reviews found: {}", reviews);
        return Result.success(reviews);
    }

    @PostMapping("/{id}/reviews")
    public Result addReview(@PathVariable Integer id, @RequestBody Review review) {
        logger.info("Adding review for game id: {}", id);
        gameService.addReview(id, review);
        return Result.success();
    }

    @PostMapping("/{id}/purchase")
    public Result purchase(@PathVariable Integer id) {
        logger.info("Purchasing game with id: {}", id);
        gameService.purchase(id);
        return Result.success();
    }
}